package com.et114.modules.cust.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.AppContext;
import com.et114.modules.cust.dao.IMng_CustDao;
import com.et114.modules.cust.dao.Mng_CustDaoImp;
import com.et114.modules.cust.services.IMng_CustService;
import com.et114.modules.cust.services.Mng_CustServiceImp;
import com.et114.modules.cust.vo.CustBaseVo;

public class JsonAction   extends BaseDispatchAction {
	
	public ActionForward deleteCust ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) ); 
		IMng_CustService mng_CustService = ( Mng_CustServiceImp )getBean ( "mng_CustService" ) ;
		CustBaseVo cust=new CustBaseVo();
		String custids=request.getParameter ( "custids" );
		
		
		int r=0;
		try{
		r=mng_CustService.deleteCustById ( cust, custids );
		}catch(Exception e){e.printStackTrace ( );}
		Map map=new HashMap(); 
		map.put ( "CUSTIDS" , custids.split ( ";" ) );
		if(r==1)
			map.put ( "RFLAG", "OK" );
		else
			map.put ( "RFLAG", "NO" );
		
		JSONArray comjson = JSONArray.fromObject ( map );
		//System.out.println ("####e"+custids+"-"+r+"--"+comjson.toString ( ));
		response.getWriter ( ).print ( comjson.toString ( ) );
		response.getWriter ( ).flush ( );
		response.getWriter ( ).close ( );
		return null;
	}
	
	public ActionForward checkCust ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) );  
		CustBaseVo cust=new CustBaseVo();
		String mibileno=request.getParameter ( "mibileno" );
		String safeno=request.getParameter ( "safeno" );
		
		if(mibileno!=null&&!mibileno.equals ( "" ))
			cust.setMobileno ( mibileno );
		else if(safeno!=null&&!safeno.equals ( "" ))
			cust.setSafeno ( safeno );
		else
			return null;
		
		//System.out.println ("--------------------"+mibileno+"\\"+safeno);
		IMng_CustDao mng_CustDao= ( Mng_CustDaoImp )getBean ( "mng_CustDao" ) ;
		List list=null;
		try {
			list = mng_CustDao.queryCustList ( cust );
		} catch ( Exception e ) {
			e.printStackTrace ( );
		}
		String result=null;
		Map map=new HashMap();
		if(list!=null&&list.size ( )>0){			
				map.put ( "RFLAG" , "FOUND" );
				map.put ( "CUSTS" , list );
		}else{
			map.put ( "RFLAG" , "NONE" );
		}
		JSONArray comjson = JSONArray.fromObject ( map );
		result=comjson.toString ( );
		log.debug ( "##json:"+result);
		response.getWriter ( ).print ( result );
		response.getWriter ( ).flush ( );
		response.getWriter ( ).close ( );
		return null;
	}
	
	public ActionForward getSupply ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) ); 
		IMng_CustService mng_CustService = ( Mng_CustServiceImp )getBean ( "mng_CustService" ) ;
		CustBaseVo cust=new CustBaseVo();
		String custids=request.getParameter ( "custids" );
		
		Map map=new HashMap(); 		
				map.put ( "USERNAME" , "aaaa" );
				map.put ( "TYPE" , "EMP" );
				String [] s={"ss","dd","cc"};
		JSONArray comjson = JSONArray.fromObject ( map );
		String result=comjson.toString ( );
		response.getWriter ( ).print ( result );
		response.getWriter ( ).flush ( );
		response.getWriter ( ).close ( );
		return null;
	}
	
}
